// pages/order/order.js
import {
  OrderModel
} from "../../models/order";
import {
  Format
}
from "../../utils/common";
const orderModel = new OrderModel()
const format = new Format()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    fold: true,
    startTime: null,
    endTime: null
  },
  onLoad: function (options) {
    console.log(options)
    if (options.endTime) {
      this.setData({
        startTime: options.startTime,
          endTime: options.endTime
      })
    }
    this.loadOrders()
  },
  loadOrders() {
    var startTime= this.data.startTime
    var endTime = this.data.endTime
    if (!startTime && !endTime) {
      var myDate = new Date();
      var tYear = myDate.getFullYear();
      var tMonth = myDate.getMonth();
      startTime = format.date("yyyy-MM", new Date()) + '-01'
      endTime = format.date("yyyy-MM", new Date()) + '-31'
    }
    orderModel.loadOrders(startTime, endTime,1).then(res => {
      this.setData({
        orders: res
      })
    })
  },
  toDetail(e) {
    let tradeNo = ''
    if (e.currentTarget.dataset.tradeNo) {
      tradeNo = e.currentTarget.dataset.tradeNo
    } else {
      tradeNo = e.currentTarget.dataset.orderId
    }
    let orderId = e.currentTarget.dataset.orderId
    let amount = e.currentTarget.dataset.createAmount
    console.log(tradeNo)
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?tradeNo=' + tradeNo + '&orderId=' +orderId + '&amount=' + amount
    })
  },
  toInvoice(e) {
    wx.navigateTo({
      url: '/pages/invioce/invoiceInformation/invoiceInformation?orderId=' + e.currentTarget.dataset.orderId + '&amount=' +  e.currentTarget.dataset.amount
    })
  },
  goCalendar() {
    wx.redirectTo({
      url: '/pages/calendar/calendar'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})